Aula 32 - Mapas (segunda parte)

Módulo 3: Apresentando suas análises

Carolina Musso

PROFESP, DEMSP, MS

Esta semana

Aula 31 - Mapas

  • O que é GIS, como usar ggplot para Mapas …

Aula 32 - Meu primeiro Mapa

  • Mapa usando shapefiles salvos no computador e outras formas.

Aula 33 - Mapas Iterativos

  • Um gostinho de mapas iterativos para dashboards

Leituras para aprofundamento

Preparação

rm(list=ls())  #limpa  o ambient
if (!require(pacman)) install.packages("pacman") #garante que tem o pacman
pacman::p_load(tidyverse, rio, sf, rnaturalearth,  geobr,
               lubridate, ggspatial, ggthemes, gsubfn, ggsn) 

pacman::p_load_gh("yutannihilation/ggsflabel")

Bases

Outra forma de ler shapefiles

brasil_bruto <- import("Exercicios/covid_br_2022.csv")

estados <- read_sf(dsn = "Exercicios/shapefiles/.",
                  layer="UFBR")

# tratando os dados
brasil_acumulado_estado <- brasil_bruto %>% 
  filter(municipio!="")%>% 
  group_by(estado) %>% 
  summarise(Acumulado=sum(casosNovos, na.rm=T))

estados_trat <- estados %>% 
  rename(estado=SIGLA) %>% 
  st_zm() # as vezes  precisa, nem sempre

covid_brasil_com_shape <- left_join(brasil_acumulado_estado, estados_trat)

Brasil

ggplot(estados_trat)+
  geom_sf(aes(geometry=geometry)) +
  north(estados_trat)

ggplot(covid_brasil_com_shape)+
  geom_sf(aes(geometry=geometry, fill=Acumulado))+
  blank()+
  north(estados_trat)

Graficos de pontos

  • Usando st_centroid()
covid_brasil_com_shape_ponto <- covid_brasil_com_shape %>% 
  mutate(centro=st_centroid(geometry))

ggplot(covid_brasil_com_shape_ponto)+
  geom_sf(aes(geometry=geometry))+
  geom_sf(aes(geometry=centro, size=Acumulado), color="red", alpha=0.5)+
  blank()

  • Ou usar geom_point com x=lat e y=long

Outras formas …

geoBr

head(list_geobr())
                    function           geography
1             `read_country`             Country
2              `read_region`              Region
3               `read_state`              States
4         `read_meso_region`         Meso region
5        `read_micro_region`        Micro region
6 `read_intermediate_region` Intermediate region
                                                                                                                               years
1 1872, 1900, 1911, 1920, 1933, 1940, 1950, 1960, 1970, 1980, 1991, 2000, 2001, 2010, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020
2                                                                   2000, 2001, 2010, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020
3 1872, 1900, 1911, 1920, 1933, 1940, 1950, 1960, 1970, 1980, 1991, 2000, 2001, 2010, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020
4                                                                   2000, 2001, 2010, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020
5                                                                   2000, 2001, 2010, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020
6                                                                                                                   2017, 2019, 2020
  source
1   IBGE
2   IBGE
3   IBGE
4   IBGE
5   IBGE
6   IBGE
regiao_geobr <- read_region() %>% 
  rename(regiao=`name_region`) %>% 
  mutate(regiao=if_else(regiao=="Centro Oeste", "Centro-Oeste", regiao))

Downloading: 5.1 kB     
Downloading: 5.1 kB     
Downloading: 19 kB     
Downloading: 19 kB     
Downloading: 91 kB     
Downloading: 91 kB     
Downloading: 91 kB     
Downloading: 91 kB     
Downloading: 200 kB     
Downloading: 200 kB     
Downloading: 290 kB     
Downloading: 290 kB     
Downloading: 390 kB     
Downloading: 390 kB     
Downloading: 390 kB     
Downloading: 390 kB     
Downloading: 520 kB     
Downloading: 520 kB     
Downloading: 540 kB     
Downloading: 540 kB     
Downloading: 560 kB     
Downloading: 560 kB     
Downloading: 590 kB     
Downloading: 590 kB     
Downloading: 590 kB     
Downloading: 590 kB     
Downloading: 710 kB     
Downloading: 710 kB     
Downloading: 780 kB     
Downloading: 780 kB     
Downloading: 780 kB     
Downloading: 780 kB     
brasil_regiao <- brasil_bruto %>% 
  filter(codmun != "") %>% 
  group_by(regiao) %>% 
  summarise(Maximo_dia=max(casosNovos, na.rm=T)) %>% 
  right_join(regiao_geobr)
ggplot(brasil_regiao, aes(fill=Maximo_dia))+
  geom_sf(aes(geometry=geom))+
  scale_fill_distiller(direction=1)

Arquivos .gpkg

map <- st_read("Exercicios/map.gpkg")
Reading layer `map' from data source 
  `/Users/carolinamusso/Desktop/R-PROFESP/Exercicios/map.gpkg' 
  using driver `GPKG'
Simple feature collection with 10 features and 2 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 4.944039 ymin: 873.8405 xmax: 20.01296 ymax: 896.602
Projected CRS: Undefined Cartesian SRS
 st_crs(map) <- 4326

 # Esse arquivo especificamente 
map_transformed <- st_transform(map, crs = 4326)

(ggmap <-  map_transformed  %>% 
   ggplot()+
   geom_sf(aes(geometry=geom, fill=hdi))+
   scale_fill_distiller(direction=1)+
   theme_map())

plot(map["hdi"])

Obrigada!